from django.http import JsonResponse
from django.shortcuts import render
from .models import *
from django.db.models import *
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
def index(request):

    num = request.GET.get('num',1)
    n = int(num)
    alls = Weather.objects.all()
    pager = Paginator(alls,30)
    try:
        p = pager.page(n)
    except PageNotAnInteger:
        p = pager.page(1)
    except EmptyPage:
        p = pager.page(pager.num_pages)
    alls = Weather.objects.values('site').annotate(c=Count('site'))
    allss = Weather.objects.filter()
    return render(request,'index.html',{'pager':pager,'p':p,'alls':alls,'allss':allss})
def aa(request):
    nian= request.GET.get('nian')
    # yue = request.GET.get('yue')
    # di = request.GET.get('di')
    articles = Weather.objects.filter(days__year=nian)
    list = []
    for i in articles:
        data = {

            'site': i.site,
            'days': i.days,
            'weather': i.weather,
            'maxs': i.maxs,
            'mins': i.mins,
            'Wind': i.Wind,
            'direction': i.direction,
        }
        list.append(data)
        print(JsonResponse(list, safe=False))
    return JsonResponse(list, safe=False)